
const db = wx.cloud.database()
const questionList = db.collection('Questionss')
let all = []
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    percent: 0,//进度条
    total: 0,
    isSelect: false,//是否选中
    sbject: [],
    userSelect: '',//记录用户最后选择的答案
    current: 1,//从第一题开始
    isParse: false,

    //以下为页面固定渲染数据
    next: "下一题",
    back: "上一题",
    like: "收藏",
    nexticon: "https://7a79-zyz7208-7gp6ayit5432f991-1304984260.tcb.qcloud.la/cloudbase-cms/upload/2022-04-28/t8a8s6f061mwllpl1txfdnod0csfb1oj_.png",
    backicon: "https://7a79-zyz7208-7gp6ayit5432f991-1304984260.tcb.qcloud.la/cloudbase-cms/upload/2022-04-28/4xd9ymnc47t5zjegqv253icl2vjnbgav_.png",
    likeicon: "https://7a79-zyz7208-7gp6ayit5432f991-1304984260.tcb.qcloud.la/cloudbase-cms/upload/2022-04-28/2cnejdb6ntdlmnal6o38hc4jx5jfu4fa_.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const db = wx.cloud.database()
    //1.获取数据的个数
    let count = await db.collection('Questionss').count()
    count = count.total
    //2.通过for循环做多次请求，并把多次请求的数据放在一个数组里

    for (let i = 0; i < count; i += 20) {//自己设置每次获取数据的量
      let list = await db.collection('Questionss').skip(i).get()
      all = all.concat(list.data)
    }
    let subject = all[0]
    this.setData({
      subject: subject,
      total: all.length
    })

  },
  //用户选择
  radioChange(e) {
    let userSelect = e.detail.value
    this.setData({
      userSelect,
      isParse: true
    })
    if (this.data.isParse) {
      this.setData({
        Parse: this.data.subject.Parse
      })
    } else {
      this.setData({
        Parse: ""
      })
    }
    //3.判断用户是否答对
    let answer2 = this.data.subject.answer
    if (answer2 == userSelect) {
      wx.showToast({
        title: '正确',
        icon: 'success'
      })
    } else {
      wx.showToast({
        title: '错误',
        icon: 'error'
      })
      //4.记录用户答错的题，方便用户查缺补漏
      let subjectNow = this.data.subject
      subjectNow.userSelect = userSelect
      db.collection('errorwork').add({
        data: {
        index : subjectNow.index,
        title : subjectNow.title,
        options : subjectNow.options,
        answer : subjectNow.answer,
        Parse : subjectNow.Parse
        }
      })
    }
  },



  //下一题
  next() {
    //1.获取用户选项并判空
    // let userSelect = this.data.userSelect
    // if (!userSelect) {
    //   wx.showToast({
    //     title: '未作答',
    //     icon: 'none'
    //   })
    //   return
    // }
    //2.如果用户有选择，就更新进度条


    let num = this.data.current + 1
    let subject = all[num - 1]
    this.setData({
      isSelect: false,
      subject,
      current: num,
      isParse: false,
      percent: (((num - 1) / all.length) * 100).toFixed(0)
    })
    if (num > all.length) {
      wx.showToast({
        title: '已经是最后一题',
        icon: 'none'
      })
      return
    }
    this.data.userSelect = ''
  },



  //上一题
  back() {
    let num = this.data.current - 1
    if (num == 0) {
      wx.showToast({
        title: '已经是第一题',
        icon: 'none'
      })
      return
    }
    let subject = all[num - 1]
    this.setData({
      isSelect: false,
      subject,
      current: num,
      isParse: false,
      percent: (((num - 1) / all.length) * 100).toFixed(0)
    })
  },

  //用户收藏
  collect() {
    let userSelect = this.data.userSelect
    wx.showToast({
      title: '收藏成功',
      icon: 'success'
    })
    let subjectNow = this.data.subject
    subjectNow.userSelect = userSelect
    db.collection('likework').add({
      data: {
        index : subjectNow.index,
        title : subjectNow.title,
        options : subjectNow.options,
        answer : subjectNow.answer,
        Parse : subjectNow.Parse
      }
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})